<?php
namespace App\Observers;
use App\User;
use App\Company;

class UserObserver {
    public function creating(User $user) {
        if(!$user->company_id){//通过注测
            $company = new Company();
            $company->name = $user->name;
            $company->save();
            $user->company_id = $company->id;
            $user->power = User::POWER_ADMIN;
        }else{//管理员后台添加
            $user->power = User::POWER_USER;
            $role = $user->role;
            $user->attachRole($role);
        }
    }

    public function updated(User $user){
        if($user->role_id){
            $user->detachRoles();
            $user->attachRole($user->role);
        }
    }
}